package com.minimalist.common.feign.fallback.lindi;

import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.lindi.vo.news.LindiNewsQueryVO;
import com.minimalist.common.entity.lindi.vo.news.LindiNewsVO;
import com.minimalist.common.feign.client.lindi.LindiNewsFeignClient;
import com.minimalist.common.feign.config.BaseFallbackFactory;
import com.minimalist.common.response.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

/**
 * 林地新闻Feign客户端降级工厂
 * 使用FallbackFactory可以获取到异常信息，便于问题排查
 *
 * @author minimalist
 */
@Component
public class LindiNewsFeignClientFallbackFactory extends BaseFallbackFactory<LindiNewsFeignClient> {
    private static final Logger log = LoggerFactory.getLogger(LindiNewsFeignClientFallbackFactory.class);


    @Override
    public LindiNewsFeignClient create(Throwable cause) {
        return new LindiNewsFeignClient() {
            @Override
            public R<?> addNews(LindiNewsVO newsVO) {
                logServiceUnavailable("添加新闻", newsVO, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), 
                        "添加新闻失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<?> updateNewsByNewsId(LindiNewsVO newsVO) {
                logServiceUnavailable("修改新闻", newsVO, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), 
                        "修改新闻失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<?> deleteNewsByNewsId(Long newsId) {
                logServiceUnavailable("删除新闻", newsId, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), 
                        "删除新闻失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<LindiNewsVO> getNewsByNewsId(Long newsId) {
                logServiceUnavailable("获取新闻详情", newsId, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), 
                        "获取新闻详情失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<PageResp<LindiNewsVO>> getPageNewsList(LindiNewsQueryVO queryVO) {
                logServiceUnavailable("分页查询新闻列表", queryVO, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), 
                        "分页查询新闻列表失败，服务不可用: " + cause.getMessage());
            }
        };
    }
}

